package band.wukong.practice.mq.activemq.hello.queue;

import band.wukong.practice.mq.activemq.hello.HelloMessageListener;
import band.wukong.practice.mq.activemq.hello.MQServerConst;
import org.apache.activemq.ActiveMQConnectionFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.jms.*;

/**
 * @author wukong(wukonggg@139.com)
 */
public class QueueReceiver {

    private static final Logger logger = LoggerFactory.getLogger(QueueReceiver.class);

    // ConnectionFactory ：连接工厂，JMS 用它创建连接
    private static final ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(
            MQServerConst.ADMIN_NAME,
            MQServerConst.ADMIN_PASSWD,
            MQServerConst.BROKER_URL);

    public static void main(String[] args) throws JMSException {
        // Connection ：JMS 客户端到JMS Provider 的连接
        final Connection connection = connectionFactory.createConnection();
        connection.start();

        // Session： 一个发送或接收消息的线程
        final Session session = connection.createSession(true, Session.AUTO_ACKNOWLEDGE);
        // Destination ：消息的目的地;消息送谁那获取.
        Destination destination = session.createQueue(MQServerConst.QUEUE_0);
        // 消费者，消息接收者
        MessageConsumer consumer1 = session.createConsumer(destination);

        consumer1.setMessageListener(new HelloMessageListener("C1", session));

    }
}
